package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class NmgnewsNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;

		String[] regex={
							"http://(?!commend).*.nmgnews.com.cn/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]+.shtml",
							"http://bbs.nmgnews.com.cn/forum.php\\?mod=viewthread&tid=[0-9]*"
							};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		String url=urlMeta.getUrl();
		if (!isDetailPage(url)) {
		}
		String html=urlMeta.getHtml();
		if (html == null) {
		}
		String title="";
		String content="";
		Long date=0l;
		int commentNum=0;
		int clickNum=0;
		NewsMeta meta=new NewsMeta();
		meta.setUrl(url);
		meta.setType(0);
		Document doc=Jsoup.parse(html);
		urlMeta=null;
		/*
		 * 解析新闻标题
		 */
		title=doc.select("h1.dcyfsh1").text();
		if(StringUtils.isBlank(title)){
			title=doc.select("span.black16_b").text();
			if(StringUtils.isBlank(title)){
				title=doc.select("td.black24_b").text();
			}
		}
		if(StringUtils.isBlank(title)){
			title=doc.select("span.STYLE3").text();
		}
		/*
		 * 解析时间
		 */
		date=DateUtils.matchDate(doc.select("h1").text());
		if(date == null){
			date=DateUtils.matchDate(doc.select("span.black12").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("font.black12").text());
				if(date == null){
					date=DateUtils.matchDate(doc.select("font.b12").text());
					if(date == null){
						date=DateUtils.matchDate(doc.select(".bsharefx01").text());
					}
				}
			}
		}
		/*
		 * 解析内容
		 */
		content=HtmlCleaner.getContentHtml(url,doc.select("div#pzoom"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("td.black14"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
			}
		}

		if(StringUtils.isBlank(title)){
			String switchUrl=StringUtils.substringBetween(html, "location.replace(\"", "\")");
			if(!StringUtils.isBlank(switchUrl)){
				urlMeta=CrawlHTML.responseToURL(switchUrl);
				html=urlMeta.getHtml();
				doc=Jsoup.parse(html);
				urlMeta=null;
				/*
				 * 解析新闻标题
				 */
				title=doc.select("span.black16_b").text();
				if(StringUtils.isBlank(title)){
					title=doc.select("td.black24_b").text();
				}
//				/*
//				 * 解析时间
//				 */
//				date=DateUtils.matchDate(doc.select("font.b12").text());
//				if(date == null){
//					date=DateUtils.matchDate(doc.select("span.black12").text());
//					if(date == null){
//						date=DateUtils.matchDate(doc.select("font.black12").text());
//						if(date == null){
//							date=DateUtils.matchDate(doc.select("h1").text());
//						}
//					}
//				}
				/*
				 * 解析内容
				 */
				content=HtmlCleaner.getContentHtml(url,doc.select("div#pzoom"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("td.black14"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
						if(StringUtils.isBlank(content))
							content=HtmlCleaner.getContentHtml(url,doc.select("div#zenwen01"));
					}
				}
			}
		}

		if(StringUtils.isBlank(title)){
			title=doc.select("title").text();
			title=StringUtils.substringBefore(title, "-");
		}
		if (StringUtils.isBlank(title)) {
		}
		/**
		 * 初始化、截取 、设置作者信息*******************************************
		 *
		 * */
		String author=null;
		author=doc.select("font[color=#000000] a").text();
		if(StringUtils.isBlank(author)){
			author=doc.select("span.black12").text();
			author=StringUtils.substringAfter(author, "来源:");
		}
		meta.setAuthor(author);
		/**
		 *                        	 **************************end *************************
		 *
		 **/
		meta.setTitle(title);
		meta.setContent(content);
		meta.setDate(date);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commentNum);

		return meta;
	}


	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		String url="http://chifeng.nmgnews.com.cn/system/2014/05/06/011456051.shtml";
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		NmgnewsNewsAnalyse a=new NmgnewsNewsAnalyse();
		boolean detailPage=a.isDetailPage(url);
		if (detailPage) {
			NewsMeta parserHtml=a.parserHtml(urlMeta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不符合正则");
		}

	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
